package com.hmall.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.ResultDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.ItemService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    /**
     * 根据id查询商品
     *
     * @param id
     * @return
     */
    @Override
    public Item selectItemById(Long id) {
        Item item = itemMapper.selectById(id);
        if (item != null) {
            System.out.println(item);
            return item;
        } else {
            throw new RuntimeException();
        }
    }

    /**
     * 新增商品
     *
     * @param item
     */
    @Override
    public void addItem(Item item) {
        Item item1 = itemMapper.selectById(item.getId());
        if (item1 == null) {
            save(item);

        } else {
            throw new RuntimeException("数据已存在,请重新添加");
        }
    }


    /**
     * 分页查询商品
     *
     * @return
     */
    @Override
    public PageDTO<Item> selectItemByPage(Integer page, Integer size) {
        Integer index = (page - 1) * size;
        List<Item> itemList = itemMapper.selectItemByPage(index, size);
        Long count = itemMapper.findCount();
        return new PageDTO<>(count, itemList);
    }

    /**
     * 物理删除商品
     *
     * @param id
     */
    @Override
    public void deleteItemById(Long id) {
        Integer status = itemMapper.selectStatusById(id);
        if (status == 2) {
            itemMapper.deleteById(id);
        }

    }

    /**
     * 根据id修改商品的状态
     *
     * @param id
     * @param status
     */
    @Override
    public void findItemStatue(Long id, Integer status) {
        if (status == 1) {
            itemMapper.updateStatusById(id, 1);
        }
        if (status == 2) {
            itemMapper.updateStatusById(id, 2);
        }
    }

    /**
     * 修改商品
     *
     * @param item
     */
    @Override
    public void updateItem(Item item) {
        Item item1 = itemMapper.selectById(item.getId());
        if (item1 != null) {
            itemMapper.updateById(item);
        } else {
            throw new RuntimeException("修改失败");
        }

    }

    /**
     * 扣减库存数量
     *
     * @param itemId
     * @param num
     */
    @Override
    public void updateStock(Long itemId, Integer num) {
        Item item = itemMapper.selectById(itemId);
        Integer stock = item.getStock();
        Integer result = stock - num;
        itemMapper.updateStockById(itemId, result);
    }

    /**
     * 回复库存
     *
     * @param itemId
     * @param num
     */
    @Override
    public void reliveStock(Long itemId, Integer num) {
        Item item = itemMapper.selectById(itemId);
        Integer stock = item.getStock();
        Integer result = stock + num;
        itemMapper.updateStockById(itemId, result);
    }


}
